package com.kkkitsch.coolalbum.util;

import java.io.IOException;
import java.io.InputStream;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import javax.net.ssl.SSLContext;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.TrustStrategy;
import org.apache.http.util.EntityUtils;
import org.springframework.web.multipart.MultipartFile;

import com.fasterxml.jackson.databind.ObjectMapper;

import sun.misc.BASE64Encoder;

public class ImageClientUtil_V {

	public static String validationImg(String image) throws Exception {

		// 客户端对象
		CloseableHttpClient httpclient = getCloseableHttpClient();

		// 百度云头像审核url
		String txurl = "https://aip.baidubce.com/api/v1/solution/direct/img_censor?access_token="
				+ ImageAuthService.getAuth();

		// POST请求和头像审核内容格式
		HttpPost httpPost = new HttpPost(txurl);
		httpPost.setHeader("Content-Type", "application/json;charset=utf-8");

		// 请求参数设置，这种方式行不通
		// List<BasicNameValuePair> nameValuePairlist = new
		// ArrayList<BasicNameValuePair>();
		// // base64字符串图片
		// nameValuePairlist.add(new BasicNameValuePair("image", image));
		//
		// String[] str = new String[] { "ocr", "politician", "antiporn",
		// "terror", "disgust", "watermark" };
		// for (String string : str) {
		// nameValuePairlist.add(new BasicNameValuePair("scenes[]", string));
		// }
		// UrlEncodedFormEntity formEntity = new
		// UrlEncodedFormEntity(nameValuePairlist, "UTF-8");
		// httpPost.setEntity(formEntity);

		// 请求参数设置
		Map<String, Object> ma = new HashMap<String, Object>();
		ma.put("image", image);
		ma.put("scenes", Arrays.asList("ocr", "politician", "antiporn", "terror", "disgust", "watermark"));
		ObjectMapper om = new ObjectMapper();
		String jsonStr = om.writeValueAsString(ma);
		// 解决中文乱码
		StringEntity stringEntity = new StringEntity(jsonStr, "UTF-8");
		httpPost.setEntity(stringEntity);

		// 请求发送
		CloseableHttpResponse response = httpclient.execute(httpPost);

		// 响应
		HttpEntity entity = response.getEntity();
		String respContent = EntityUtils.toString(entity, "UTF-8").trim();
		httpPost.abort();
		return respContent;
	}

	public static String validation(MultipartFile file) throws Exception {
		InputStream inputStream = null;
		byte[] data = null;
		try {
			inputStream = file.getInputStream();
			data = new byte[inputStream.available()];
			inputStream.read(data);
			inputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		// 加密
		BASE64Encoder encoder = new BASE64Encoder();
		String encodeImgString = encoder.encode(data);
		return validationImg(encodeImgString);
	}

	public static CloseableHttpClient getCloseableHttpClient() {
		// 客户端对象
		CloseableHttpClient httpclient = null;
		try {
			SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
				public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
					return true;
				}
			}).build();
			SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
			// 创建一个可以访问https的请求对象
			httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
			return httpclient;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}